define([ "dojo/_base/declare", "widgets/baseWidget/BaseWidget",
    "dojo/text!./templates/Template.html","dojo/_base/fx","dojo/fx","dojo/fx/easing", "dojo/dom",
    "dojo/on","dojo/aspect","dojo/query","dijit/layout/ContentPane","dijit/layout/TabContainer","dojo/parser",
    "dojo/topic","dojo/_base/lang","dijit/layout/TabContainer" ,"dojo/dom-construct","dojo/dom-class"
    ],
    function(declare,BaseWidget, template,baseFx,fx,easing,Dom,on,aspect,query,ContentPane,TabContainer,Parser,
    			Topic,lang,TabContainer,domConstruct,domClass) {
	 
        return declare([ BaseWidget], {
        	 
            templateString : template,
            tabCom:null,
            hasExpand:true, 
            panelWidth:260,
            relatedom:null,
            
            
            constructor : function() { 
            	this.inherited(arguments);
            },
            //组件初始化方法
            postCreate : function() {
            	
            	this.inherited(arguments);
            	domConstruct.place(this.domNode, dojo.body());
            	this.tabCom=new TabContainer({region: 'center', tabPosition: 'top',useSlider:false});
            	this.tabCom.placeAt(this.tab).startup();
            	this.setWidth();//设置宽度
            	on(this.expandBtn,"click",dojo.hitch(this,this.expandLeft));
            	on(this.category,"click",dojo.hitch(this,this.dataClick));
            	on(this.category,"mouseover",dojo.hitch(this,this.changeStyle));
            	Topic.subscribe("removealltab", lang.hitch(this, this.removeAllTab));
            	Topic.subscribe("expandLeft", lang.hitch(this, this.expandLeft));
            	Topic.subscribe("closeTab", lang.hitch(this, this.closeTab));
            },
            startup:function(){
            	this.inherited(arguments);
            },
            addTab:function(widget,title,closable,onClose){
            	//debugger;
            	if(this.getTab(title)){
            		
            	}else{
            		this.setWidthSmall();
            		widget.title=title;
                	widget.closable=closable;
                	widget.onClose=onClose||this.onClose;
                	this.tabCom.addChild(widget);
            	}
            	this.tabCom.selectChild(widget);
            	this.setWidth();
            },
            removeTab:function(widget){
            	if(this.getTab(widget.title)){
            		this.tabCom.removeChild(widget);
            		this.setWidth();
            	}
            },
            
            removeAllTab:function(){
            	var tabs=this.tabCom.getChildren();
            	var temtabs = this.tabCom;
            	dojo.forEach(tabs,function(item){
            		if(item.closable){
            			item.onClose();
            			temtabs.removeChild(item);
            		}
            	});
            	this.setWidth();
            },
            
            selectTab:function(widget){
            	if(this.tabCom.hasChildren(widget)){
            		this.tabCom.selectChild(widget);
            	}
            },
            getTab:function(title){
            	var tabs=this.tabCom.getChildren();
            	var finditem = null;
            	dojo.forEach(tabs,function(item){
            		if(item.title==title){
            			finditem = item;
            			return;
            		}
            	});
            	return finditem;
            },
            onClose:function(e){
            	return true;
            },
            
            setWidthSmall:function(){
            	var doms = query(".dijitTabInner");
            	var length = doms.length;
            	if (length < 4){
            		for(var i = 0; i < length; i++){
            			var dom = doms[i];
            			dojo.style(dom,"width","20px");
            		}
            	}
            },
            
            setWidth:function(){
            	//debugger;
            	var doms = query(".dijitTabInner");
            	var length = doms.length;
            	if (length > 4){
            		
            	}else{
            		var percent = parseFloat(1/length)*Global.layerPanelWidth;
            		for(var i = 0; i < length; i++){
            			var dom = doms[i];
            			dojo.style(dom,"width",percent+"px");
            		}
            	}
            },
            
            closeTab:function(title){
            	this.setWidth();
            },
            
            dataClick:function(e){
            	console.log(e.target.innerText);
            	Topic.publish("detailExpand", e);
            },
            
            expandLeft:function(evt,force){
            	this.hasExpand=force!=undefined?force:!this.hasExpand;
            	var tempdom = null;
            	if (this.relate){//如果有连动的dom节点
            		tempdom=query(this.relate)[0];
            	}
            	tempdom=query('div.mapWidgetRoot')[0];
            	// var tempdom=$(query(".mainArea", this.domNode)[0]);
            	var anim=fx.combine([
					baseFx.animateProperty({
					    //easing: easing.bounceOut,
					    //duration: 500,
					    node: this.domNode,
					    properties: {
					        left: this.hasExpand?0:-this.panelWidth
					    }
					}),
					baseFx.animateProperty({
					    node: tempdom,
					    properties: {
					        left: this.hasExpand?this.panelWidth:0
					    }
					})
            	]);
            	aspect.after(anim, "onEnd", dojo.hitch(this,function(){
            		//this.expandBtn.src=this.hasExpand?"images/unfold.png":"images/fold.png";
            		Topic.publish('leftExpand',this.expandLeft.hasExpand);
            	}));
            	this.expandBtn.src=this.hasExpand?"images/unfold.png":"images/fold.png";
            	anim.play();
            	
            },
            
            changeStyle:function(evt){
            	var test = this.domNode;
    			domClass.remove(evt, "alt");
        		domClass.add(evt, "alt2");
            },
            
            destroy:function(){
            	this.inherited(arguments);
            }
        });
    });
